package Underworld_Survival;

import java.util.ArrayList;
import java.util.List;

import static Underworld_Survival.Game.gameOutput;
import static Underworld_Survival.Game.stage;

public class EventManager {
    private final List<GameEvent> events=new ArrayList<>();
    private GameEvent forcedNextEvent;//强制的下一个事件

    //注册事件
    public void registerEvent(GameEvent event){
        events.add(event);
    }
    //触发随机事件（或者下一个确定的事件）
    public void triggerEvent(Player player){
        if(forcedNextEvent!=null){//下一个事件已经确定了
            gameOutput.println(GameOutput.MsgType.EVENT,forcedNextEvent.getName());
            forcedNextEvent.execute(player);
            forcedNextEvent=null;
        }else{
            WeightedRandomSelector<GameEvent > selector = new WeightedRandomSelector<>();//使用权重随机选择器，随机事件
            List<GameEvent> eventList;
            if(stage%5==0){
                eventList=new ArrayList<>(){{add(new purifyingSpringSource_Event());}};
            }
            else if(stage<=15){//前期
                PeriodConfig<GameEvent > earlyEvent = EventConfig.earlyPeriod();//前期随机事件（随机出一种）
                eventList = selector.select(earlyEvent);//用list列表接受随机出来的事件
            }else if(stage==16){
                eventList=new ArrayList<>(){{add(new boss_Are_Coming());}};
            }else if(stage>15&&stage<=30){//中期
                PeriodConfig<GameEvent > midEvent = EventConfig.midPeriod();//中期期随机事件（随机出一种）
                eventList = selector.select(midEvent);
            }
            else if(stage==31){
                eventList=new ArrayList<>(){{add(new boss_Are_Coming());}};
            }else if(stage>30&&stage<=45){//后期
                PeriodConfig<GameEvent > lateEvent = EventConfig.latePeriod();//后期随机事件（随机出一种）
                eventList = selector.select(lateEvent);
            }else if(stage==46){
                eventList=new ArrayList<>(){{add(new boss_Are_Coming());}};
            }else if(stage>45&&stage<=60){//末期
                PeriodConfig<GameEvent > lateEvent = EventConfig.finalPeriod();//后期随机事件（随机出一种）
                eventList = selector.select(lateEvent);
            }else if(stage==61){
                eventList=new ArrayList<>(){{add(new boss_Are_Coming());}};
            }
            else{//60关以后，无尽模式
                PeriodConfig<GameEvent > endlessModeEvent = EventConfig.endlessMode();//无尽模式随机事件（随机出一种）
                eventList = selector.select(endlessModeEvent);
            }
            gameOutput.println(GameOutput.MsgType.EVENT,eventList.get(0).getName());
            eventList.get(0).execute(player);//发生事件
        }
    }
    //选定触发事件
    public void triggerFixedEvent(GameEvent event,Player player){event.execute(player);}
    //设置下一个强制事件
    public void setForcedNextEvent(GameEvent event){
        this.forcedNextEvent=event;
    }

}
